package com.jbookshelf.dal.providers;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jbookshelf.dal.entities.Genres;

@Repository
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public class GenresDataProviderImp implements GenresDataProvider{
	
	
	@Autowired
	SessionFactory sessionFactory;
	

	@Override
	@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
	public List<Genres> getGenres() {
		Session s = sessionFactory.getCurrentSession();
		Query q  = s.createQuery("from Genres g ");
		return q.list();
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void addGenres(Genres genre) {
        Session s = sessionFactory.getCurrentSession();
        s.save(genre);
        
		
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void updateGenres(Genres genre) {
        Session s = sessionFactory.getCurrentSession();
        s.update(genre);
		
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void deleteGenres(Genres genre) {
        Session s = sessionFactory.getCurrentSession();
        s.delete(genre);
		
	}

	@Override
	@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
	public List<Genres> getGenreByName(String genreName) {
		Session session = sessionFactory.getCurrentSession();
		Query query  = session.createQuery("from Genres g where g.name = :genreName");
		query.setParameter("genreName", genreName);
		return query.list();
		
	}

}
